# $Id: X.py 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from django.db.models import Q

class X(object):
    def __init__(self, field):
        self.field = field

    def _eval(self, other, lookup):
        return Q(**{ '%s__%s' % ( self.field, lookup ) : other })

    def __eq__(self, other):
        return self._eval(True, 'isnull') if other is None else self._eval(
              other, 'exact')

    def __ne__(self, other):
        return ~ ( self == other )

    def __lt__(self, other):
        return self._eval(other, 'lt')

    def __le__(self, other):
        return self._eval(other, 'lte')

    def __gt__(self, other):
        return self._eval(other, 'gt')
    
    def __ge__(self, other):
        return self._eval(other, 'gte')

    def __mod__(self, other):
        return self._eval(other, 'icontains')

    def __lshift__(self, other):
        return self._eval(other, 'istartswith')

    def __rshift__(self, other):
        return self._eval(other, 'iendswith')

    def __or__(self, other):
        return self._eval(other, 'in')
